#ifndef BASE_CTTCPCLIENT_H_
#define BASE_CTTCPCLIENT_H_

#include "ct_thread.h"
#include "ctType.h"
#include "ctSocket.h"
#include "ctTime.h"
#include <string>

class ctTCPClient : public ctThread, public ctProcessor
{
        public:
                ctTCPClient(
                                const std::string &name,
                                const std::string &ip = "127.0.0.1",
                                const WORD port = 80,
                                const bool compress = false) 
                        : ctThread(name),ip(ip),port(port),pSocket(NULL),compress(compress) {};

                ~ctTCPClient() 
                {
                        close();
                }

                bool connect(const std::string &bindip="");

                bool connect(const char *ip,const WORD port, const std::string &bindip="")
                {
                        this->ip = ip;
                        this->port = port;
                        return connect(bindip);
                }

                virtual void close()
                {
                        SAFE_DELETE( pSocket );

                }

                virtual bool sendCmd(const void *pstrCmd,const int nCmdLen);

                void setIP(const char *ip)
                {
                        this->ip = ip;
                }

                const char *getIP() const
                {
                        return ip.c_str();
                }

                void setPort(const WORD port)
                {
                        this->port = port;
                }

                const WORD getPort() const
                {
                        return port;
                }

                virtual void run();

        protected:
                std::string ip;
                WORD port;    
                ctSocket *pSocket;

                const bool compress; 

}; 

class ctTCPBufferClient : public ctTCPClient
{
        public:
                ctTCPBufferClient(
                                const std::string &name,
                                const std::string &ip = "127.0.0.1",
                                const WORD port = 80,
                                const bool compress = false,
                                const int usleep_time = 50000) 
                        : ctTCPClient(name,ip,port,compress),usleep_time(usleep_time),_buffered(false) { }

                void close()
                {
                        sync();
                        ctTCPClient::close();
                }

                void run();
                bool sendCmd(const void *pstrCmd,const int nCmdLen);
                void setUsleepTime(const int utime)
                {
                        usleep_time = utime;
                }

        private :
                bool ListeningRecv();
                bool ListeningSend();
                void sync();

                DWORD usleep_time;
                volatile bool _buffered;
};

#endif //BASE_CTTCPCLIENT_H_
